﻿
using DualPlaneFR4Paster.Models;
using Microsoft.EntityFrameworkCore;

namespace DualPlaneFR4Paster
{
    public class SysContext : DbContext
    {
        public DbSet<MPartNum> MPartNums { get; set; }
        public DbSet<MSystemParam> MSystemParams { get; set; }
        public DbSet<XYZRPoint> XYZRPoints { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            string DbPath = System.IO.Path.Join(path, "SystemParam.db");
            options.UseLazyLoadingProxies().UseSqlite($"Data Source={DbPath};Pooling=False");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<MPartNum>(entity => {
                entity.Property(e => e.PartNum).IsRequired(true).HasMaxLength(32);
            });
            modelBuilder.Entity<MSystemParam>(entity => {
                entity.Property(e => e.Name).IsRequired(true).HasMaxLength(32);
                entity.Property(e => e.Value).IsRequired(true).HasMaxLength(64);
            });
            modelBuilder.Entity<XYZRPoint>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired(true).HasMaxLength(32);
                entity.Property(e => e.Alias).IsRequired(false).HasMaxLength(64);
            });
            modelBuilder.Entity<MSystemParam>().HasData(
                new MSystemParam
                {
                    Id = 1,
                    Name = "PartNum",
                    Value = "F107318A1"
                }
                );
            modelBuilder.Entity<MPartNum>().HasData(
                new MPartNum
                {
                    Id = 1,
                    PartNum = "F107045b0"
                }
                );
            modelBuilder.Entity<MPartNum>().HasData(
                new MPartNum
                {
                    Id = 2,
                    PartNum = "F107318A1"
                }
                );
        }
    }
}
